<template>
  <div class="login-container">
    <el-form ref="loginForm"
             :model="loginForm"
             :rules="loginRules"
             class="login-form"
             auto-complete="on"
             label-position="left">
      <h3 class="title">找回密码</h3>
      <el-form-item prop="phone">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input type="tel"
                  v-model="loginForm.phone"
                  name="phone"
                  maxlength="11"
                  auto-complete="on"
                  placeholder="请确认输入手机号"
                  @keyup.enter.native="handleLogin" />
      </el-form-item>
      <el-form-item class="codeForm"
                    prop='code'>
        <div class="codeInput">
          <span class="svg-container">
            <svg-icon icon-class="code" />
          </span>
          <el-input placeholder="请输入验证码"
                    v-model="loginForm.code" />
        </div>

        <span class="show-pwd send_code">
          <send-code :phone='loginForm.phone' />
        </span>
      </el-form-item>
      <el-form-item prop="pass">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input :type="pwdType"
                  v-model="loginForm.pass"
                  name="password"
                  auto-complete="on"
                  placeholder="请输入密码"
                  @keyup.enter.native="handleLogin" />
        <span class="show-pwd"
              @click="showPwd">
          <svg-icon icon-class="eye" />
        </span>
      </el-form-item>
      <el-form-item prop="qpass">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input :type="ConfPwdType"
                  v-model="loginForm.qpass"
                  name="password"
                  auto-complete="on"
                  placeholder="请确认输入密码"
                  @keyup.enter.native="handleLogin" />
        <span class="show-pwd"
              @click="showConfirmPwd">
          <svg-icon icon-class="eye" />
        </span>
      </el-form-item>
      <el-form-item>
        <el-button :loading="loading"
                   type="primary"
                   style="width:100%;"
                   @click.native.prevent="handleLogin">
          找回
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { validatPhone } from '@/utils/validate'
import sendCode from '@/components/sendCode/index'
export default {
  name: 'forget',
  components: { sendCode },
  data () {
    const validatePhone = (rule, value, callback) => {
      if (validatPhone(value)) {
        callback()
      } else {
        callback(new Error('您输入的手机号有误,请重新输入'))
      }
    }
    const validateCode = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入验证码'))
      } else {
        callback()
      }
    }
    const validatePwd = (rule, value, callback) => {
      if (this.loginForm.pass === value) {
        callback()
      } else {
        callback(new Error('两次密码不一致,请重新输入'))
      }
    }
    const validatePass = (rule, value, callback) => {
      if (!value) {
        callback(new Error('密码不能为空'))
      } else {
        callback()
      }
    }

    return {
      loginForm: {
        pass: '',
        code: '',
        qpass: '',
        phone: ''
      },
      loginRules: { //自定义from表单规则验证
        phone: [{ required: true, trigger: 'blur', validator: validatePhone }],
        qpass: [{ required: true, trigger: 'blur', validator: validatePwd }],
        pass: [{ required: true, trigger: 'blur', validator: validatePass }],
        code: [{ required: true, trigger: 'blur', validator: validateCode }],
      },
      loading: false,
      pwdType: 'password',
      ConfPwdType: 'password',
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd () {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
    },
    showConfirmPwd () {
      if (this.ConfPwdType === 'password') {
        this.ConfPwdType = ''
      } else {
        this.ConfPwdType = 'password'
      }
    },
    handleLogin () {
      this.$refs.loginForm.validate(async valid => {  //登录 form验证
        if (valid) {
          this.loading = true
          const res = await this.api.forget(this.loginForm)  //修改密码
          if (res.code == 1) {
            this.loading = false
            this.$message({ message: res.msg, type: 'success' })
            this.$router.push('/')
          } else {
            this.loading = false
            this.$message.error(res.msg)
          }
        }
      })
    }
  },
  mounted () {

  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg: #2d3a4b;
$light_gray: #eee;

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: #fff !important;
      }
    }
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
  .Retrieve-pwd {
    background: none;
    margin-bottom: 0;
  }

  .codeForm {
    border: none;
    background: none;
    .codeInput {
      width: 70%;
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }
    & .show-pwd {
      height: 97%;
      right: 0 !important;
      top: 0 !important;
    }
  }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background-color: $bg;
  .login-form {
    position: absolute;
    left: 0;
    right: 0;
    width: 520px;
    max-width: 100%;
    padding: 35px 35px 15px 35px;
    margin: 120px auto;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .title {
    font-size: 26px;
    font-weight: 400;
    color: $light_gray;
    margin: 0px auto 40px auto;
    text-align: center;
    font-weight: bold;
  }
  .send_code {
    height: 94%;
    button {
      height: 100%;
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: none;
      color: #fff;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .Retrieve-pwd {
    border: none;
    text-align: right;
  }
}
</style>
